package org.locker.config;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.locker.domain.WechatMpInfo;
import org.locker.handler.ScanHandler;
import org.locker.handler.SubscribeHandler;
import org.locker.handler.UnsubscribeHandler;
import org.locker.mapper.WechatMpInfoMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;

import static me.chanjar.weixin.common.api.WxConsts.EventType;
import static me.chanjar.weixin.common.api.WxConsts.EventType.SUBSCRIBE;
import static me.chanjar.weixin.common.api.WxConsts.EventType.UNSUBSCRIBE;
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;

/**
 * wechat mp configuration
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@AllArgsConstructor
@Configuration
public class WxMpConfiguration {
    private final UnsubscribeHandler unsubscribeHandler;
    private final SubscribeHandler subscribeHandler;
    private final ScanHandler scanHandler;

    @Resource
    private WechatMpInfoMapper wechatMpInfoMapper;

    @Bean
    public WxMpService wxMpService() {
        // 代码里 getConfigs()处报错的同学，请注意仔细阅读项目说明，你的IDE需要引入lombok插件！！！！
        List<WechatMpInfo> configs = wechatMpInfoMapper.selectList(new LambdaQueryWrapper<WechatMpInfo>().eq(WechatMpInfo::getStatus, 1));
        WxMpService service = new WxMpServiceImpl();
        if(CollUtil.isNotEmpty(configs)){
            service.setMultiConfigStorages(configs
                    .stream().map(a -> {
                        WxMpDefaultConfigImpl configStorage= new WxMpDefaultConfigImpl();
                        configStorage.setAppId(a.getAppId());
                        configStorage.setSecret(a.getAppSecret());
                        configStorage.setToken(a.getToken());
                        configStorage.setAesKey(a.getEncodingAesKey());
                        return configStorage;
                    }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
        }
        return service;
    }

    @Bean
    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
        // 关注事件
        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
        // 取消关注事件
        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
        // 扫码事件
        newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
        return newRouter;
    }

}
